package com.example.yin.controller;

import com.example.yin.common.ErrorMessage;
import com.example.yin.common.SuccessMessage;
import com.example.yin.common.aop.MyLog;
import com.example.yin.config.OSSConfiguration;
import com.example.yin.constant.Constants;
import com.example.yin.domain.Song;
import com.example.yin.event.UploadMessage;
import com.example.yin.service.OssService;
import com.example.yin.service.impl.SongServiceImpl;
import com.example.yin.utils.RedisCache;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ObjectUtils.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
public class SongController {

    @Autowired
    private SongServiceImpl songService;

    @Autowired
    private OSSConfiguration ossConfiguration;

    @Autowired
    private OssService ossService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
        factory.setMaxFileSize(DataSize.of(20, DataUnit.MEGABYTES));
        // 设置总上传数据总大小10M
        factory.setMaxRequestSize(DataSize.of(20, DataUnit.MEGABYTES));
        return factory.createMultipartConfig();
    }

    @Configuration
    public static class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/songPic/**")
                    .addResourceLocations(Constants.SONG_PIC_PATH);
            registry.addResourceHandler("/song/**")
                    .addResourceLocations(Constants.SONG_PATH);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/song/add/test", method = RequestMethod.POST)
    public Object addSongtest(HttpServletRequest req, @RequestParam("file") MultipartFile mpfile) {
        String name = req.getParameter("name").trim();
        ossService.uploadFile(mpfile,"test/"+name+".mp3");
        return new SuccessMessage<String>("上传成功").getMessage();
    }


    // 添加歌曲
    @MyLog(value = "上传歌曲")
    @ResponseBody
    @RequestMapping(value = "/song/add", method = RequestMethod.POST)
    public Object addSong(HttpServletRequest req, @RequestParam("file") MultipartFile mpfile,@RequestParam("singerId") Integer singerId) {
        //从请求中获取
        String name = req.getParameter("name").trim();
        String introduction = req.getParameter("introduction").trim();
        String lyric = req.getParameter("lyric").trim();
        String fileName = mpfile.getOriginalFilename();
        String storeUrlPath = ossConfiguration.getSongFilePath()+fileName;
        String pic = req.getParameter("picPath").trim();

        //上传oss
        ossService.uploadFile(mpfile,fileName);


        //存数据库
        Song song = new Song();
        song.setSingerId(singerId);
        song.setName(name);
        song.setIntroduction(introduction);
        song.setCreateTime(new Date());
        song.setUpdateTime(new Date());
        song.setPic(pic);
        song.setLyric(lyric);
        song.setUrl("/"+storeUrlPath);
        boolean res = songService.addSong(song);

        UploadMessage uploadMessage = new UploadMessage();
        uploadMessage.setSongId(song.getId());
        uploadMessage.setFilepath(storeUrlPath);
        uploadMessage.setFile(mpfile);
        uploadMessage.setTags(Arrays.asList(1,2,3,4));

        applicationEventPublisher.publishEvent(uploadMessage);

        if (res) {
            return new SuccessMessage<String>("上传成功", storeUrlPath).getMessage();
        } else {
            return new ErrorMessage("上传失败").getMessage();
        }
    }

    // 删除歌曲
    @RequestMapping(value = "/song/delete", method = RequestMethod.GET)
    public Object deleteSong(HttpServletRequest req) {
        String id = req.getParameter("id");

        boolean res = songService.deleteSong(Integer.parseInt(id));
        if (res) {
            return new SuccessMessage<Null>("删除成功").getMessage();
        } else {
            return new ErrorMessage("删除失败").getMessage();
        }
    }

    // 返回所有歌曲
    @RequestMapping(value = "/song", method = RequestMethod.GET)
    public Object allSong() {

        return new SuccessMessage<List<Song>>(null, songService.allSong()).getMessage();
    }

    // 返回指定歌曲ID的歌曲
    @RequestMapping(value = "/song/detail", method = RequestMethod.GET)
    public Object songOfId(HttpServletRequest req) {
        String id = req.getParameter("id");

        return new SuccessMessage<List<Song>>(null, songService.songOfId(Integer.parseInt(id))).getMessage();
    }

    // 返回指定歌手ID的歌曲
    @RequestMapping(value = "/song/singer/detail", method = RequestMethod.GET)
    public Object songOfSingerId(HttpServletRequest req) {
        String singerId = req.getParameter("singerId");



        return new SuccessMessage<List<Song>>(null, songService.songOfSingerId(Integer.parseInt(singerId))).getMessage();
    }
    // 返回指定歌手ID的歌曲
    @RequestMapping(value = "/song/singer/detail/test", method = RequestMethod.GET)
    public Object songOfSingerIdtest(HttpServletRequest req) {
        String singerId = req.getParameter("singerId");

        List<Song> songs = songService.songOfSingerId(Integer.parseInt(singerId));
        for(Song song : songs){
            song.setUrl(ossConfiguration.getFullUrl()+song.getUrl());
        }

        return new SuccessMessage<List<Song>>(null, songService.songOfSingerId(Integer.parseInt(singerId))).getMessage();
    }

    // 返回指定歌手名的歌曲
    @RequestMapping(value = "/song/singerName/detail", method = RequestMethod.GET)
    public Object songOfSingerName(HttpServletRequest req) {
        String name = req.getParameter("name");

        return new SuccessMessage<List<Song>>(null, songService.songOfSingerName('%' + name + '%')).getMessage();
    }

    // 更新歌曲信息
    @MyLog(value = "更新歌曲信息")
    @ResponseBody
    @RequestMapping(value = "/song/update", method = RequestMethod.POST)
    public Object updateSongMsg(Integer id,Integer singerId,String name,String introduction,String lyric) {
//        String id = req.getParameter("id").trim();
//        String singer_id = req.getParameter("singerId").trim();
//        String name = req.getParameter("name").trim();
//        String introduction = req.getParameter("introduction").trim();
//        String lyric = req.getParameter("lyric").trim();

        Song song = new Song();
//        song.setId(Integer.parseInt(id));
//        song.setSingerId(Integer.parseInt(singer_id));
        song.setId(id);
        song.setSingerId(singerId);
        song.setName(name);
        song.setIntroduction(introduction);
        song.setUpdateTime(new Date());
        song.setLyric(lyric);

        boolean res = songService.updateSongMsg(song);
        if (res) {
            return new SuccessMessage<Null>("修改成功").getMessage();
        } else {
            return new ErrorMessage("修改失败").getMessage();
        }
    }

    // 更新歌曲图片
    @MyLog(value = "更新歌曲图片")
    @ResponseBody
    @RequestMapping(value = "/song/img/update", method = RequestMethod.POST)
    public Object updateSongPic(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        String fileName = System.currentTimeMillis() + urlFile.getOriginalFilename();
        String storeUrlPath = "/img/songPic/" + fileName;

        ossService.uploadFile(urlFile,ossConfiguration.getSingerPicPath()+fileName);

        Song song = new Song();
        song.setId(id);
        song.setPic(storeUrlPath);
        boolean res = songService.updateSongPic(song);
        if (res) {
            return new SuccessMessage<String>("上传成功", storeUrlPath).getMessage();
        } else {
            return new ErrorMessage("上传失败").getMessage();
        }
    }

    // 更新歌曲
    @MyLog(value = "更新歌曲音频")
    @ResponseBody
    @RequestMapping(value = "/song/url/update", method = RequestMethod.POST)
    public Object updateSongUrl(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        String fileName = urlFile.getOriginalFilename();
        String storeUrlPath = "/song/" + fileName;

        ossService.uploadFile(urlFile,ossConfiguration.getSingerPicPath()+fileName);
        Song song = new Song();
        song.setId(id);
        song.setUrl(storeUrlPath);
        boolean res = songService.updateSongUrl(song);
        if (res) {
            return new SuccessMessage<String>("更新成功", storeUrlPath).getMessage();
        } else {
            return new ErrorMessage("更新失败").getMessage();
        }
    }

    @GetMapping(value = "/song/hit")
    public Object getHit(@RequestParam("id") Integer id){
        Integer res = 0;
        if(redisCache.getCacheObject("song:"+id)==null){
            res = songService.searchHit(id);
            res++;
            redisCache.setCacheObject("song:"+id,res);
        }
        else{
            res = redisCache.getCacheObject("song:"+id);
            res++;
            redisCache.setCacheObject("song:"+id,res);
        }
        return new SuccessMessage<Integer>("成功", res).getMessage();
    }

    @GetMapping(value = "/song/user")
    public Object getUserSongs(@RequestParam("id") Integer id){
        List<Song> songs = songService.songOfuserId(id);
        if(songs.isEmpty()){
            return  new ErrorMessage("查询为空").getMessage();
        }
        else{
            return new SuccessMessage<List<Song>>("成功", songs).getMessage();
        }
    }

    @GetMapping(value = "/song/similar")
    public  Object getSimilar(@RequestParam("id") Integer id){
        PageHelper.startPage(1,3);
        List<Song> songs = songService.songsOfSimilar((id));
        if(songs.isEmpty()){
            return  new ErrorMessage("查询为空").getMessage();
        }
        else{
            return new SuccessMessage<List<Song>>("成功", songs).getMessage();
        }
    }


}
